/*******************************************************************************************************

Figure A7: Sensitivity to the explicit calibration of the extensive margin in the log
specification

*******************************************************************************************************/

*Set directory path
*cd "C:\Users\tsa23\Documents\petitions\replication files"
cd "C:\Users\tsa23\Dropbox\Swing_riots\petitions\stata\replication files"


clear all

*read in our main dataset
use dta/did_rep.dta

*Elminate contrituencies which are not part of case G
drop if ID == 45 //University of Cambridge
drop if ID == 191 //University of Oxford
keep if new == 1 | drummond == .

drop if year==1830 | year==1831 | year==1832

gen treatgroup = 0
replace treatgroup =  1 if new==1 

gen treatinter = treatperiod*treatgroup

label var treatinter "T * A" 
label var treatgroup "T"
label var treatperiod "A"

* Define a macro with the list of calibration values
local cal_vals "0 1 2 3 4 5 6 7 8"

* Create a temporary file to store results
tempfile resultsfile
postfile handle cal_val treat_coef lb ub using "`resultsfile'", replace

* Loop over the calibration values from the list
foreach cal_val of local cal_vals {
    preserve
        * Create a calibrated version of petitions and log-transform it
        gen petitions_cal = petitions
        replace petitions_cal = `cal_val' if petitions_cal == 0
        gen ln_petitions_cal = log(petitions_cal)
        
        * Run fixed-effects regression with clustered standard errors
        xtreg ln_petitions_cal treatinter $control i.year, fe cluster(ID)
        
        * Extract coefficient and standard error for treatinter
        local beta = _b[treatinter]
        local se = _se[treatinter]
        
        * Compute the critical value for a 90% CI (two-tailed, 5% each tail)
        local crit = invttail(e(df_r), 0.05)
        
        * Calculate lower and upper bounds of the 90% CI
        local lower = `beta' - `crit' * `se'
        local upper = `beta' + `crit' * `se'
        
        * Post the results for this calibration value
        post handle (`cal_val') (`beta') (`lower') (`upper')
    restore
}
postclose handle
preserve
* Load the posted results into memory
use "`resultsfile'", clear

label var treat_coef "Coefficient" 
label var lb "90% CI"
label var ub " "
 
set scheme plotplainblind
* Generate a plot of treatinter coefficients vs. calibration values with 90% CI error bars
twoway (rcap lb ub cal_val) (scatter treat_coef cal_val), ///
       xlabel(`cal_vals') ///
       xtitle("Calibrated Extensive Margin Value") ///
       ytitle("ATE estimate") ///
       title(" ")
	   graph export graphs\figureA7a.pdf, as(pdf) name("Graph")  replace

  restore

  


clear all

*read in our main dataset
use dta/did_rep.dta

*Elminate contrituencies which are not part of case L
drop if ID == 45 //University of Cambridge
drop if ID == 191 //University of Oxford
keep if sch_A==1 | sch_B == 1 | drummond!=.

drop if year==1830 | year==1831 | year==1832

gen treatgroup = 0
replace treatgroup =  1 if sch_A==1 

gen treatinter = treatperiod*treatgroup

label var treatinter "T * A" 
label var treatgroup "T"
label var treatperiod "A"



* Define a macro with the list of calibration values
local cal_vals "0 0.5 1 1.5 2 2.5 3 3.5 4"

* Create a temporary file to store results
tempfile resultsfile
postfile handle cal_val treat_coef lb ub using "`resultsfile'", replace

* Loop over the calibration values from the list
foreach cal_val of local cal_vals {
    preserve
        * Create a calibrated version of petitions and log-transform it
        gen petitions_cal = petitions
        replace petitions_cal = `cal_val' if petitions_cal == 0
        gen ln_petitions_cal = log(petitions_cal)
        
        * Run fixed-effects regression with clustered standard errors
        xtreg ln_petitions_cal treatinter $control i.year, fe cluster(ID)
        
        * Extract coefficient and standard error for treatinter
        local beta = _b[treatinter]
        local se = _se[treatinter]
        
        * Compute the critical value for a 90% CI (two-tailed, 5% each tail)
        local crit = invttail(e(df_r), 0.05)
        
        * Calculate lower and upper bounds of the 90% CI
        local lower = `beta' - `crit' * `se'
        local upper = `beta' + `crit' * `se'
        
        * Post the results for this calibration value
        post handle (`cal_val') (`beta') (`lower') (`upper')
    restore
}
postclose handle
preserve
* Load the posted results into memory
use "`resultsfile'", clear

label var treat_coef "Coefficient" 
label var lb "90% CI"
label var ub " "
 
set scheme plotplainblind
* Generate a plot of treatinter coefficients vs. calibration values with 90% CI error bars
twoway (rcap lb ub cal_val) (scatter treat_coef cal_val), ///
       xlabel(`cal_vals') ///
       xtitle("Calibrated Extensive Margin Value") ///
       ytitle("ATE estimate") ///
       title(" ")
	   graph export graphs\figureA7b.pdf, as(pdf) name("Graph")  replace

  restore

  
